package com.mds.group.four.test;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class Preview extends SurfaceView implements SurfaceHolder.Callback {
	SurfaceHolder mHolder;
	Camera mCamera;
	
	Preview(Context context){
		super(context);
		mHolder = getHolder();
		mHolder.addCallback(this);
		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}
	
	public void surfaceCreated(SurfaceHolder holder){
		mCamera = Camera.open();
		try {
			mCamera.setPreviewDisplay(holder);
		} catch (IOException ioe) {
			mCamera.release();
			mCamera = null;
		}
	}
	
	public void surfaceDestroyed(SurfaceHolder holder){
		mCamera.stopPreview();
		mCamera.release();
		mCamera = null;
	}
	
	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h){
		Camera.Parameters parameters = mCamera.getParameters();
		parameters.setPreviewSize(w, h);
		mCamera.setParameters(parameters);
		mCamera.startPreview();
	}
}
